package cn.livingCloud.controller.content;

import cn.livingCloud.dto.ArticlesReplyRuleDto;
import cn.livingCloud.dto.ArticlesReplyRuleFormDto;
import cn.livingCloud.service.ReplyRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;

/**
 * 图文消息
 * Created by 唐旭 on 2015/12/26.
 */
@Controller
@RequestMapping("/admin/wechat/news")
public class ArticlesReplyRuleController {
    @Autowired
    private ReplyRuleService ruleService;

    @RequestMapping("/detail.htm")
    public String detail(String uuid, Model model) {
        ArticlesReplyRuleDto formDto = ruleService.loadArticlesReplyRuleDto(uuid);
        model.addAttribute("formDto", formDto);
        return "wechat/reply_rule/news/detail";
    }

    /**
     * 图文消息 编辑
     */
    @RequestMapping("/edit.htm")
    public String load(String uuid, Model model) {
        ArticlesReplyRuleFormDto formDto = ruleService.loadArticlesReplyRuleFormDto(uuid);
        model.addAttribute("formDto", formDto);
        return "wechat/reply_rule/news/edit";
    }

    /**
     * 图文消息 提交编辑内容
     */
    @RequestMapping(value = "/edit.htm", method = RequestMethod.POST)
    public String submitForm(@ModelAttribute("formDto") @Valid ArticlesReplyRuleFormDto formDto, BindingResult result) {
        if (result.hasErrors()) {
            return "wechat/reply_rule/news/edit";
        }
        ruleService.updateArticlesReplyRule(formDto);
        return "redirect:/admin/wechat/reply_rule_list.htm?edit=success";
    }


}
